##这个配置文件演示添加Install功能的CMakeLists.txt写法

# CMake 最低版本号要求
cmake_minimum_required (VERSION 3.4.1)
# 设置项目名称及版本号
project(Lesson VERSION 1.0)
# 或者
#project(Lesson)
#set (Lesson_VERSION_MAJOR 1)
#set (Lesson_VERSION_MINOR 0)

# 设置USE_MYMATH是否生效开关
option(USE_MYMATH "Use Lesson provided math implementation" ON)
#option(USE_MYMATH "Use Lesson provided math implementation" OFF)

# 生成配置文件
#configure_file(config.h.in config.h) #这会在config.h.in同级目录下生成config.h
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h") # 这会在PROJECT_BINARY_DIR目录下生成config.h

# 指定 C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 如果USE_MYMATH打开则用math的数学函数
if(USE_MYMATH)
    add_subdirectory(math)
    list(APPEND EXTRA_LIBS MathFunctions)
endif()

# 生成目标可执行文件
add_executable(Lesson main2.cpp)
# 为Lesson链接math目录下的MathFunctions静态库
target_link_libraries(Lesson PUBLIC ${EXTRA_LIBS})
# 包含的头文件，要在add_xxx之后添加。包含生成的config.h
target_include_directories(Lesson PUBLIC
        "${PROJECT_BINARY_DIR}"
        )

# adding install rules. 产生 Makefile 后需要执行make install才会执行安装
#for MathFunctions we want to install the library and header file and for the application we want to install the executable and configured header.
set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/Lesson") #设置安装目录，默认为系统应用安装目录。/usr/local on UNIX and c:/Program Files/${PROJECT_NAME} on Windows.
install(TARGETS Lesson DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h"
        DESTINATION include
        )